package com.itheima;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//ServleContextDemo的演示
public class ServletContextDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1获取ServletContext对象
        ServletContext context=getServletContext();
        //2常用方法的演示
        //获取全局配置参数：getInitParameter(String key)根据key获取value
        String value = getInitParameter("globalDesc");
        System.out.println(value);

        //获取应用的虚拟目录：getContextPath()
        String contextPath = context.getContextPath();
        System.out.println(contextPath);
        //根据虚拟目录获取绝对路径：getRealPath(String path)
        String realPath = context.getRealPath("/");
        System.out.println(realPath);
        String b = context.getRealPath("/b.txt");
        System.out.println(b);
        String c = context.getRealPath("/WEB-INF/c.txt");
        System.out.println(c);
        String a = context.getRealPath("/WEB-INF/classes/a.txt");
        System.out.println(a);
        //设置共享数据
        context.setAttribute("username","zahngsan");
        //删除共享数据
        context.removeAttribute("username");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
